import pymysql

db = pymysql.connect(
    host="localhost",
    port=3306,
    user='root',    #在这里输入用户名
    password='root',     #在这里输入密码
    charset='utf8' ,
    database='studb'
    ) #连接数据库

cursor = db.cursor() #创建游标对象

with open("g:/UserBehavior.csv",'r') as f:
     while True:
         line = f.readline().strip()
         split = line.split(",")
         print(split)
         sql="insert into userbehavior(userid,item_id,category_id,type,times) values({0},{1},{2},'{3}',{4})"\
             .format(split[0],split[1],split[2],split[3],split[4])
         cursor.execute(sql)
         if not line:
             break


#cursor.execute(sql)  #执行sql语句

cursor.close()
db.close()  #关闭数据库的连接
